package com.weige.conver.service;

import com.alibaba.fastjson.JSONObject;
import com.weige.conver.entity.*;
import com.weige.conver.utils.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@EnableScheduling
public class EnvDataService {

    @Autowired
    private RabbitMQService rabbitMQService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private SigService sigService;
    @Autowired
    private RedisUtils redisUtils;
    private ConfigService configService = new ConfigService();

    /**
     * 传到参数封装成EnvData对象-数据类型
     *
     * @param receiveMessage
     * @param model          模型
     * @param sigIdTail      信号量尾部类型
     * @param sigName        信号量名称
     * @param sigType        信号量类型
     * @return
     * @throws IOException
     */
    public EnvData getEnvDataByData(ReceiveMessage receiveMessage, String model, String sigIdTail, String sigName, String sigType, String sigUnit) throws IOException, InterruptedException {
        Map siteMap = configService.getResouceData();
        if (siteMap.isEmpty()){
            siteMap = FileUtils.getBaseMap();
        }
        String deviceId = receiveMessage.getDevId();
        String deviceName = receiveMessage.getDevName();
        String data = receiveMessage.getValue();
        String desc = receiveMessage.getDesc();
        //********

        PosInfo posInfo = new PosInfo();
        posInfo.setSiteId((String) siteMap.get("siteId"));
        posInfo.setSiteName((String) siteMap.get("siteName"));
        posInfo.setObjectId(deviceId);
        posInfo.setObjectName(deviceName);
        posInfo.setMonitorDate(DateUtils.getNowTime());
        posInfo.setObjectType(model);
        //判断sigId在sig.json文件中是否已存在，不存在则添加
        Sig sig = new Sig();
        String sigId = (String) redisUtils.getKeyByHash("sigList",deviceId);
        if (StringUtils.isBlank(sigId)) {
            sigId = sigService.addSigId(deviceId);
        }
        sigId = sigId.substring(1,sigId.length()-1);
        String newSigId = sigId+sigIdTail;
        sig.setSigId(newSigId);
        sig.setSigName(sigName);
        sig.setSigValue(StringSubUtils.dec(data));
        sig.setSigType(sigType);
        sig.setSigUnit(sigUnit);
        sig.setSigDescription(desc);
        List<Sig> sigList = new ArrayList<>();
        sigList.add(sig);
        posInfo.setPoints(sigList);
        List<PosInfo> posInfoList = new ArrayList<>();
        posInfoList.add(posInfo);
        EnvData env = new EnvData();
        env.setEnvDataList(posInfoList);
        env.setTopicName("EnvMonitor_envDataService");
        return env;
    }

    /**
     * 传到参数封装成EnvData对象-报警类型
     *
     * @param receiveMessage
     * @param model          模型
     * @param sigIdTail      信号量尾部类型
     * @param sigName        信号量名称
     * @param sigType        信号量类型
     * @return
     * @throws IOException
     */
    public EnvData getEnvDataByAction(ReceiveMessage receiveMessage, String model, String sigIdTail, String sigName, String sigType, String sigUnit) throws IOException, InterruptedException {
        Map siteMap = configService.getResouceData();
        if (siteMap.isEmpty()){
            siteMap = FileUtils.getBaseMap();
        }
        String deviceId = receiveMessage.getDevId();
        String deviceName = receiveMessage.getDevName();
        String data = receiveMessage.getValue();
        String desc = receiveMessage.getDesc();
        //********
        PosInfo posInfo = new PosInfo();
        posInfo.setSiteId((String) siteMap.get("siteId"));
        posInfo.setSiteName((String) siteMap.get("siteName"));
        posInfo.setObjectId(deviceId);
        posInfo.setObjectName(deviceName);
        posInfo.setMonitorDate(DateUtils.getNowTime());
        posInfo.setObjectType(model);
        //判断sigId在sig.json文件中是否已存在，不存在则添加
        Sig sig = new Sig();
        String sigId = (String) redisUtils.getKeyByHash("sigList",deviceId);
        if (StringUtils.isBlank(sigId)) {
            sigId = sigService.addSigId(deviceId);
        }
        sigId = sigId.substring(1,sigId.length()-1);
        String newSigId = sigId+sigIdTail;
        sig.setSigId(newSigId);
        sig.setSigName(sigName);
        sig.setSigValue(data);
        sig.setSigType(sigType);
        sig.setSigUnit(sigUnit);
        sig.setSigDescription(desc);
        List<Sig> sigList = new ArrayList<>();
        sigList.add(sig);
        posInfo.setPoints(sigList);
        List<PosInfo> posInfoList = new ArrayList<>();
        posInfoList.add(posInfo);
        EnvData env = new EnvData();
        env.setEnvDataList(posInfoList);
        env.setTopicName("EnvMonitor_envDataService");
        return env;
    }

    /**
     * 传到参数封装成EnvData对象-控制器类型
     *
     * @param receiveMessage
     * @param model          模型
     * @param sigIdTail      信号量尾部类型
     * @param sigName        信号量名称
     * @param sigType        信号量类型
     * @return
     * @throws IOException
     */
//    public EnvData getEnvDataByControl(ReceiveMessage receiveMessage, String model, String sigIdTail, String sigName, String sigType, String sigUnit) throws IOException, InterruptedException {
//
//        String deviceId = receiveMessage.getDevId();
//        String deviceName = receiveMessage.getDevName();
//        String data = receiveMessage.getValue();
//        String desc = receiveMessage.getDesc();
//        //********
//        Map siteMap = FileUtils.getBaseMap();
//
//        PosInfo posInfo = new PosInfo();
//        posInfo.setSiteId((String) siteMap.get("siteId"));
//        posInfo.setSiteName((String) siteMap.get("siteName"));
//        posInfo.setObjectId(deviceId);
//        posInfo.setObjectName(deviceName);
//        posInfo.setMonitorDate(DateUtils.getNowTime());
//        posInfo.setObjectType(model);
//        //判断sigId在sig.json文件中是否已存在，不存在则添加
//        Sig sig = new Sig();
//        String sigId = (String) redisUtils.getKeyByHash("sigList",deviceId);
//        if (StringUtils.isBlank(sigId)) {
//            sigId = sigService.addSigId(deviceId);
//        }
//        sigId = sigId.substring(1,sigId.length()-1);
//        String newSigId = sigId+sigIdTail;
//        sig.setSigId(newSigId);
//        sig.setSigName(sigName);
//        sig.setSigValue(data);
//        sig.setSigType(sigType);
//        sig.setSigUnit(sigUnit);
//        sig.setSigDescription(desc);
//        List<Sig> sigList = new ArrayList<>();
//        sigList.add(sig);
//        posInfo.setPoints(sigList);
//        List<PosInfo> posInfoList = new ArrayList<>();
//        posInfoList.add(posInfo);
//        EnvData env = new EnvData();
//        env.setEnvDataList(posInfoList);
//        env.setTopicName("EnvMonitor_envDataService");
//        return env;
//    }

    /**
     * 10s往物联网平台推送动环主控设备数据
     */
    @Scheduled(fixedRate = 10000)
    public void sendMainEnvDeviceToMQ() throws IOException {
        rabbitMQService.sendData("0", "gzj_gpz_env_master", "110kV光谱站智能网关动环主控设备", "env_master", "ShuyanyuanAIGateway", "envDataService");
    }


    /**
     * 获取动环设备列表
     *
     * @return
     * @throws IOException
     */
    public String getAllEnvDevices() throws IOException {
        Map<String, ReceiveMessage> map = ConverService.envDataMap;
        Map siteMap = FileUtils.getBaseMap();
        List<EnvObject> envObjectList = deviceService.getDataList(map);
        JSONObject result = new JSONObject();
        result.put("returnCode", 200);
        result.put("returnMsg", "环境监测类设备列表");
        result.put("siteId", siteMap.get("siteId"));
        result.put("siteName", siteMap.get("siteName"));
        result.put("jobectList", envObjectList);
        return JSONObject.toJSONString(result);
    }


}
